package com.gzhryc.shared_device.common.device.dao;

import com.gzhryc.jdbc.BaseDao;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.Query;
import com.gzhryc.shared_device.common.device.dao.db.Device;
import com.gzhryc.shared_device.common.device.dao.models.DeviceTypeCount;
import com.gzhryc.shared_device.common.device.dao.models.PlaceDeviceCount;

import java.sql.SQLException;
import java.util.List;

public class DeviceDao extends BaseDao<Device> {

    public DeviceDao(String jdbcKey) {
        super(jdbcKey);
    }

    public List<String> findDistinctDeviceTypeId(Conditions conditions) throws SQLException {
        String sql = "SELECT DISTINCT device_type_id from " + this.tableName;
        Query query = new Query(sql);
        query.appendSql(conditions.toSql(query.getParams(),true));
        return this.jdbcHelper.findFirstField(query,String.class);
    }

    public List<String> findDeviceSn(Conditions conditions) throws SQLException {
        String sql = "SELECT device_sn from " + this.tableName;
        Query query = new Query(sql);
        query.appendSql(conditions.toSql(query.getParams(),true));
        return this.jdbcHelper.findFirstField(query,String.class);
    }

    public List<DeviceTypeCount> countGroupDeviceTypeId(Conditions conditions) throws SQLException {
        String sql = "SELECT device_type_id AS deviceTypeId,COUNT(device_sn) AS deviceCount from " + this.tableName;
        Query query = new Query(sql);
        query.appendSql(conditions.toSql(query.getParams(),true));
        query.appendSql(" GROUP BY device_type_id");
        return this.jdbcHelper.find(query,DeviceTypeCount.class);
    }

    public List<PlaceDeviceCount> countGroupPlaceId(Conditions conditions) throws SQLException {
        String sql = "SELECT place_id AS placeId,COUNT(device_sn) AS deviceCount from " + this.tableName;
        Query query = new Query(sql);
        query.appendSql(conditions.toSql(query.getParams(),true));
        query.appendSql(" GROUP BY place_id");
        return this.jdbcHelper.find(query,PlaceDeviceCount.class);
    }
}
